package com.leilei.educms.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leilei.commonutils.R;
import com.leilei.educms.entity.CrmBanner;
import com.leilei.educms.entity.vo.BannerQuery;
import com.leilei.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.crypto.engines.RC4Engine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author testjava
 * @since 2023-05-09
 */
@RestController
@RequestMapping("/educms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //1、分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        bannerService.page(bannerPage,null);
        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }


    //2 添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok();
    }


    //3 修改banner
    @ApiOperation(value = "修改Banner")
    @PutMapping("updateBanner")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    //4 删除banner
    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R removeBanner(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }


    //5 根据id查询
    @ApiOperation(value = "获取Banner")
    @GetMapping("getBanner/{id}")
    public R getBanner(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner", banner);
    }


    /**
     * 4、条件查询带分页的方法
     */
    @PostMapping("pageBannerCondition/{current}/{limit}")
    public R pageBannerCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) BannerQuery bannerQuery){

        //创建page对象
        Page<CrmBanner> pageBanner = new Page<>(current,limit);

        //构造条件
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper();

        //多条件组合查询
        //判断条件值是否为空，如果不为空拼接条件
        String title = bannerQuery.getTitle();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();

        if (!StringUtils.isEmpty(title)){
            //构建条件
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(begin)){
            //构建条件
            queryWrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //构建条件
            queryWrapper.le("gmt_create",end);
        }

        //排序
        queryWrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        bannerService.page(pageBanner,queryWrapper);

        long total = pageBanner.getTotal();    //总记录数
        List<CrmBanner> records = pageBanner.getRecords();     //数据list集合

        return R.ok().data("total",total).data("rows",records);

    }
}

